package com.xms.sdk.core;

import org.joda.time.DateTime;

import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 * DynamicData
 * </p>
 *
 * @author migo
 * @since 2021/9/1
 */
public class DynamicData extends HashMap<String, Object> {
    public DynamicData(){}
    public DynamicData(HashMap<String, Object> map)
    {
        super(map);
    }

    public DynamicData setValue(String key, Object value)
    {
        if (!this.containsKey(key))
        {
            this.put(key, value);
        }
        else{
            this.replace(key, value);
        }
        return this;
    }

    public Object getValue(String key){
        if(this.containsKey(key)){
            return this.get(key);
        }
        return null;
    }

    public int getIntValue(String key, int defaultIfNull){
        Object value = getValue(key);
        return value != null && !value.toString().isEmpty() ? Integer.parseInt(value.toString()) : defaultIfNull;
    }

    public String getStringValue(String key){
        if(this.containsKey(key)){
            return this.get(key).toString();
        }
        return "";
    }

    public int getIntValue(String key){
        return Integer.parseInt(this.getStringValue(key));
    }

    public float getFloatValue(String key){
        return Float.parseFloat(this.getStringValue(key));
    }

    public boolean getBoolValue(String key){
        return Boolean.parseBoolean(this.getStringValue(key));
    }

    public UUID getUUIDValue(String key){
        return UUID.fromString(this.getStringValue(key));
    }

    public DateTime getDateValue(String key){
        return DateTime.parse(this.getStringValue(key));
    }
}
